package com.canbot.u05.utils;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.widget.Toast;


/**
 * 可根据Config.DEBUG开关show 可在子线程show
 */
public class ToastTools {

        private Toast toast;

        private Handler handler;

        private Runnable showRunnable;

        private String text;

        private ToastTools(final Context context) {
                this.handler = new Handler(Looper.getMainLooper());
                this.showRunnable = new Runnable() {
                        @Override
                        public void run() {
                                if (ToastTools.this.toast == null) {
                                        ToastTools.this.toast = Toast.makeText(context, "", Toast.LENGTH_SHORT);
                                }
                                ToastTools.this.toast.setText(ToastTools.this.text);
                                ToastTools.this.toast.show();
                        }
                };
        }

        public static ToastTools newInstance(Context context) {
                return new ToastTools(context);
        }

        public boolean showText(String text) {
                this.text = text;
                if (true) {
                        handler.post(showRunnable);
                        return true;
                }
                return false;
        }
}
